/** 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.free.i.virtual.machine.hal.devicealias.controller;

import java.util.Observable;
import java.util.Observer;

import javax.management.Notification;
import javax.management.NotificationListener;

/**  
 * This is the class observed by all the device alias port: this object collect all the notifications
 * fired by every device activated
 * @author stefano
 */
public final class DeviceAliasPortMessageListener extends Observable implements NotificationListener {

	private static final long serialVersionUID = 1206637208827008757L;
	private static DeviceAliasPortMessageListener DEVICE_ALIAS_PORT_MESSAGE_LISTENER_INSTANCE = null;
	//private static Logger logger = Logger.getLogger(DeviceAliasPortMessageListener.class);
	
	/**
	 * Private as for a singleton
	 */
	private DeviceAliasPortMessageListener() {}

	/**
	 * Factory singleton private instance accessor
	 * @return THE instance of this factory
	 */
	public static DeviceAliasPortMessageListener getInstance(){
		if(DEVICE_ALIAS_PORT_MESSAGE_LISTENER_INSTANCE == null){
		   synchronized (DeviceAliasPortMessageListener.class){
			   if(DEVICE_ALIAS_PORT_MESSAGE_LISTENER_INSTANCE == null){
				  DEVICE_ALIAS_PORT_MESSAGE_LISTENER_INSTANCE = new DeviceAliasPortMessageListener();
			   }	   
		   }
		}
		return DEVICE_ALIAS_PORT_MESSAGE_LISTENER_INSTANCE;
	}
	
	/*
	public void update(Observable deviceAliasPort, Object message) {
		logger.info("Device alias port command, message received from [" + deviceAliasPort + "], now digesting");
		DeviceAliasPortMessage deviceAliasPortMessage = (DeviceAliasPortMessage) message;
		digestMessage(deviceAliasPortMessage);
	}
    */
	
	public void setObserver(Observer observer){
		super.addObserver(observer);
	}
	
	/**
	 * Handle notifications received by device alias ports
	 * This is also the call back collector.
	 * @param notification is the notification received
	 * @param handback is the opaque object set when listener was set
	 */
	public void handleNotification(Notification notification, Object handback){  
       
		//Immediately forward the call to the collector that with a clock 
		//create per unit of time readable virtual devices snapshots
		super.setChanged();
		super.notifyObservers(notification);
		//logger.info("Notification proxy -> RX: [" + notification + "], by [" + handback + "]");
	}
		
}
